package com.app.controller.importExcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.app.common.AdminPermissionImportTableAnalysisEventListener;
import com.app.mapper.MilitaryNewSportsMapper;
import com.app.model.AdminUserModel;
import com.app.model.MilitarySportsNewModel;
import com.app.model.YearTaskModel;
import com.app.service.AdminUserService;
import com.app.service.MilitarySportsService;
import com.app.service.MilitaryTrainService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 导入 军事
 */
@Slf4j
@RestController
@Controller
@RequestMapping("/api/import")
public class ImportMilitaryController {

    @Autowired
    private MilitarySportsService militarySportsService;

    @Autowired
    private MilitaryTrainService militaryTrainService;
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private MilitaryNewSportsMapper militaryNewSportsMapper;

    //导入军事体育信息
    @PostMapping("/importSports")
    public Result<Object> importMilitarySports(@RequestParam(value = "file") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        assert originalFilename != null;
        String lastName = originalFilename.substring(originalFilename.lastIndexOf("."));
        if (!lastName.equals(".xlsx") && !lastName.equals(".xls")) {
            return Result.error(201, "请上excel文件");
        }
        long size = file.getSize();
        if (size > 1024 * 1024 * 500) {
            return Result.error(201, "文件大小超过了500MB");
        }
        String mes = militarySportsService.importMilitarySports(file);
        return Result.success(mes);
    }

    //导入军事体育信息
    @PostMapping("/importNewSports")
    public Result<Object> importMilitarySportsNew(@RequestParam(value = "file") MultipartFile file/*, HttpServletRequest request*/) throws IOException {
        String originalFilename = file.getOriginalFilename();
        assert originalFilename != null;
        String lastName = originalFilename.substring(originalFilename.lastIndexOf("."));
        if (!lastName.equals(".xlsx") && !lastName.equals(".xls")) {
            return Result.error(201, "请上excel文件");
        }
        long size = file.getSize();
        if (size > 1024 * 1024 * 500) {
            return Result.error(201, "文件大小超过了500MB");
        }
        String mes = militarySportsService.importMilitarySportsNew(file);
        return Result.success(mes);
    }


    //导入军事训练综合评定表
    @PostMapping("/importTrain")
    public Result<Object> importMilitaryTrain(@RequestParam(value = "file") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        assert originalFilename != null;
        String lastName = originalFilename.substring(originalFilename.lastIndexOf("."));
        if (!lastName.equals(".xlsx") && !lastName.equals(".xls")) {
            return Result.error(201, "请上excel文件");
        }
        long size = file.getSize();
        if (size > 1024 * 1024 * 500) {
            return Result.error(201, "文件大小超过了500MB");
        }
        String mes = militaryTrainService.importMilitaryTrain(file);
        return Result.success("");
    }
}
